package com.cangxuan.app.ui.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.app.R;
import com.cangxuan.app.adapter.HomeCollectionListAdapter;
import com.cangxuan.app.entity.Collections;
import com.cangxuan.app.ui.collection.CollectionDetailActivity;
import com.cangxuan.app.utils.HttpUtils;
import com.cangxuan.app.utils.ListViewUtils;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * 主页藏品列表页面
 * author:何程城
 */
public class HomeListFragment extends Fragment {

    ListView listView;
    View parentView;
    HomeCollectionListAdapter homeCollectionListAdapter;
    Handler handler = new Handler();
    List<Collections> collectionList;
    ScrollView homeScrollView;
    int pageIndex = 1;
    boolean isFinish = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (parentView == null) {
            parentView = inflater.inflate(R.layout.fragment_home_list, container, false);
            listView = parentView.findViewById(R.id.home_collections);
            init();
            initCollectionList();
        }
        return parentView;
    }

    public void init() {
        collectionList = new ArrayList<>();
        homeCollectionListAdapter = new HomeCollectionListAdapter(collectionList,getContext());
        listView.setAdapter(homeCollectionListAdapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = new Intent();
            intent.setClass(getContext(), CollectionDetailActivity.class);
            intent.putExtra("collection", collectionList.get(i));
            startActivity(intent);
        });

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    public void initCollectionList() {
        ProgressDialog dialog = ProgressDialog.show(getContext(), "", "加载中...");
        new Thread(() -> {
            String jsonString = HttpUtils.get("/api/home/collections/"+pageIndex+"/10");
            JSONObject jsonResponse = JSONObject.parseObject(jsonString);
            if (jsonResponse == null){
                handler.post(() -> {
                    dialog.dismiss();
                    Toast.makeText(getContext(), "服务器异常", Toast.LENGTH_SHORT).show();
                });
            }else if (jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK) {
                JSONArray jsonRecords = jsonResponse.getJSONArray("value");
                for (int i = 0; i < jsonRecords.size(); i++) {
                    JSONObject jsonGoods = jsonRecords.getJSONObject(i);
                    Collections collections = new Collections(
                            jsonGoods.getInteger("id"),
                            jsonGoods.getString("name"),
                            jsonGoods.getInteger("mintage"),
                            jsonGoods.getString("issuer"),
                            jsonGoods.getString("image"),
                            (float)jsonGoods.getInteger("price")
                    );
                    collectionList.add(collections);
                }
                handler.post(() -> {
                    dialog.dismiss();
                    homeCollectionListAdapter.notifyDataSetChanged();
                    ListViewUtils.setListViewHeightBasedOnChildren(listView);
                });
            } else {
                handler.post(() -> {
                    dialog.dismiss();
                    Toast.makeText(getContext(), jsonResponse.getString("message"), Toast.LENGTH_SHORT).show();
                });
            }
        }).start();
    }

}
